package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.DictionaryService;
import com.zero.clinic.service.RegisterService;
import com.zero.clinic.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/register")
public class RegisterController {
    @Resource
    private RegisterService registerService;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * @function 进入挂号费管理模块
     * @return
     */
    @RequiresPermissions("register:registerPage")
    @RequestMapping("/registerPage")
    public String registerPage(){
        return "registerPage";
    }

    /**
     * @function 挂号费列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("register:list")
    @RequestMapping("/list")
    public PageInfo<Register> registerList(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Register信息
        RegisterExample example = new RegisterExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 通过挂号费名称查询
            RegisterExample.Criteria criteria1 = example.createCriteria();
            criteria1.andRegisterNameLike("%"+ keywords +"%");

            // 2.2 根据挂号费类型名称查询
            RegisterExample.Criteria criteria2 = example.or();
            criteria2.andRegisterTypeNameLike("%"+ keywords +"%");
        }
        List<Register> registers = registerService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Register> pageInfo = new PageInfo<>(registers);
        return pageInfo;
    }

    /**
     * @function 编辑挂号费管理页面
     * @param registerId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String registerEdit(Long registerId, Model m){

        // 1.如果前端页面需要更新挂号费信息，则会传一个需要更新的registerId过来。这里需要通过registerId查出更新信息后共享给前端页面
        if (registerId != null) {
            Register register = registerService.selectByPrimaryKey(registerId);
            m.addAttribute("register",register);
        }

        // 2.查询出所有的挂号分类，然后共享给前端页面
        List<Dictionary> registerTypes = dictionaryService.selectDictionariesByParentName(Constant.DICTIONARY_REGISTER_TYPE);
        m.addAttribute("registerTypes", registerTypes);

        // 3.查询出所有的挂号子费用分类，然后共享给前端页面
        List<Dictionary> registerSubTypes = dictionaryService.selectDictionariesByParentName(Constant.DICTIONARY_REGISTER_SUB_TYPE);
        m.addAttribute("registerSubTypes", registerSubTypes);

        return "registerEdit";
    }

    /**
     * @function 获取挂号费子费用详细信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/subs")
    @RequiresPermissions("register:subs")
    public List<RegisterSub> registerSubDetail(Long registerId){
        List<RegisterSub> registerSubs = registerService.getRegisterSubsByRegisterId(registerId);
        return registerSubs;
    }

    /**
     * @function 新增挂号费项
     * @param register
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("register:insert")
    public MessageObject registerInsert(@RequestBody Register register){  //@RequestBody 接收来自前端的json数据，然后转成对象
        System.out.println("需要新增的挂号费：" + register);
        System.out.println("需要新增的挂号费子费用：" + register.getRegisterSubs());
        //1.插入数据
        int row = registerService.insert(register);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新挂号费项
     * @param register
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("register:update")
    public MessageObject registerUpdate(@RequestBody Register register){
        //1.更新数据
        System.out.println("需要更新的挂号费：" + register);
        int row = registerService.updateByPrimaryKeySelective(register);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查挂号费名称是否重复
     * @param registerName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkRegisterName")
    @RequiresPermissions("register:checkRegisterName")
    public boolean registerCheckRegisterName(String registerName){
        System.out.println(registerName);
        RegisterExample example = new RegisterExample();
        RegisterExample.Criteria criteria = example.createCriteria();
        criteria.andRegisterNameEqualTo(registerName);
        List<Register> registers = registerService.selectByExample(example);
        return registers.size() > 0 ? false : true;
    }

    /**
     * @function 删除挂号费项
     * @param registerId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("register:delete")
    public MessageObject registerDelete(Long registerId){
        //3.根据registerId删除数据
        int row = registerService.deleteByPrimaryKey(registerId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除挂号费
     * @param registerIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("register:batchdel")
    public MessageObject registerBatchDelete(String registerIds){
        String[] registerIdsArr = registerIds.split(",");
        //1.根据registerIdsArr删除数据
        int row = registerService.deleteByIds(registerIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }


}
